package com.example.j280_webproject.day037;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/servlet")
public class ServletXinXi extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String age = req.getParameter("age");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        JDBC_ServletXinXi jdbc_servletXinXi = new JDBC_ServletXinXi();
        if (username!=""&password==""&age==""){
            try {
                ResultSet resultSet = jdbc_servletXinXi.select(username);
                writer.print("id"+"\t"+"用户名"+"\t"+"密码"+"\t"+"年龄" + "<br>");
                while (resultSet.next()){
                    writer.println(resultSet.getInt("id"));
                    writer.println(resultSet.getString("username"));
                    writer.println(resultSet.getString("password"));
                    writer.println(resultSet.getInt("age") + "<br>");
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
                writer.print("系统繁忙请稍后再试！");
            }
        }
        else if (username!=""&password!=""&age==""){
            try {
                ResultSet resultSet = jdbc_servletXinXi.select(username,password);
                writer.print("id"+"\t"+"用户名"+"\t"+"密码"+"\t"+"年龄" + "<br>");
                while (resultSet.next()){
                    writer.println(resultSet.getInt("id"));
                    writer.println(resultSet.getString("username"));
                    writer.println(resultSet.getString("password"));
                    writer.println(resultSet.getInt("age") + "<br>");
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
                writer.print("系统繁忙请稍后再试！");
            }
        }
        else if (username!=""&password!=""&age!=""){
            try {
                ResultSet resultSet = jdbc_servletXinXi.select(username,password,Integer.parseInt(age));
                writer.print("id"+"\t"+"用户名"+"\t"+"密码"+"\t"+"年龄" + "<br>");
                while (resultSet.next()){
                    writer.println(resultSet.getInt("id"));
                    writer.println(resultSet.getString("username"));
                    writer.println(resultSet.getString("password"));
                    writer.println(resultSet.getInt("age") + "<br>");
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
                writer.print("系统繁忙请稍后再试！");
            }
        }
        else if(username==""&password==""&age==""){
            try {
                ResultSet resultSet = jdbc_servletXinXi.select();
                writer.print("id"+"\t"+"用户名"+"\t"+"密码"+"\t"+"年龄" + "<br>");
                while (resultSet.next()){
                    writer.println(resultSet.getInt("id"));
                    writer.println(resultSet.getString("username"));
                    writer.println(resultSet.getString("password"));
                    writer.println(resultSet.getInt("age") + "<br>");
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
                writer.print("系统繁忙请稍后再试！");
            }
        }
    }
}
